package com.tn.socket;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

/**
 * @ClassName ChatClient
 * @Description TODO
 * @Author Yezhongzhu
 * @Date 2022/1/4 14:43
 * @Version v1.0
 **/
public class ChatClient extends Frame{
    Socket s = null;
    OutputStream dos = null;
    InputStream dis = null;
    private boolean bConnected = false;

    TextField tfTxt = new TextField();
    TextArea taContent = new TextArea();

    Thread tRecv = new Thread(new RecvThread());

    public static void main(String[] args) {
        new ChatClient().launchFrame("127.0.0.1",8982);
    }

    public void launchFrame(String host,int port) {
        setLocation(400, 300);
        this.setSize(300, 300);
        add(tfTxt, BorderLayout.SOUTH);
        add(taContent, BorderLayout.NORTH);
        pack();
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent arg0) {
                disconnect();
                System.exit(0);
            }
        });
        tfTxt.addActionListener(new TFListener());
        setVisible(true);
        connect(host,port);

        tRecv.start();
    }

    public void connect(String host,int port) {
        try {
            s = new Socket(host, port);
            dos = s.getOutputStream();
            dis = s.getInputStream();
            System.out.println("~~~~~~~~连接成功~~~~~~~~!");
            bConnected = true;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void disconnect() {
        try {
            dos.close();
            dis.close();
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private class TFListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            String str = tfTxt.getText().trim();
            tfTxt.setText("");

            BufferedWriter writer = null;
            try {
                dos.write(str.getBytes());
                dos.flush();
//                writer = new BufferedWriter(new OutputStreamWriter(dos));
//                writer.write(str);
//                writer.newLine();
//                writer.flush();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }

    }

    private class RecvThread implements Runnable {

        public void run() {
                while (bConnected) {
                    String str = "";
                    try {
                        //不仅仅是起到勺子舀输入流里面数据的作用，也是存储数据
                        byte [] b=new byte[1024];
                        StringBuffer sb=new StringBuffer();
                        int len=0;
                        String s=null;
                        if((len=dis.read(b))!=-1){
                            s=new String(b);
                            sb.append(s);
                        }
                        str = sb.toString();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    taContent.setText(taContent.getText() + str + '\n');
                }

        }

    }
}
